package cn.dai.advice;

import cn.dai.exception.TokenAuthException;
import cn.dai.pojo.ResultData;
import cn.dai.pojo.ReturnCode;
import cn.dai.exception.ResponseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {
    /**
     * 默认全局异常处理。
     *
     * @param e the e
     * @return ResultData
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultData<String> exception(Exception e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        return ResultData.fail(ReturnCode.FAIL.getStatus(), e.getMessage());
    }

    /**
     * 默认全局异常处理。
     *
     * @param e the e
     * @return ResultData
     */
    @ExceptionHandler(ResponseException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultData<String> runtimeException(ResponseException e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        return ResultData.fail(e.getStatus(), e.getMessage());
    }

    /**
     * 默认全局异常处理。
     *
     * @param e the e
     * @return ResultData
     */
    @ExceptionHandler(TokenAuthException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResultData<String> tokenAuthException(TokenAuthException e) {
        log.error("token异常信息 ex={}", e.getMessage(), e);
        return ResultData.fail(e.getStatus(), e.getMessage());
    }

}
